package Components;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import smash_tris.*;

public class RoomsPanel extends JPanel {
	
	private ArrayList<Room> rooms;
	private ArrayList<ImageButton> buttons;
	private String roomIcon;
	private Player user;
	private JPanel motherPanel;
	
	private int width = 300;
	private int heigh = 80;
	
	public RoomsPanel(ArrayList<Room> rooms, JPanel mP) {
		
		this.setLayout(null);
		this.motherPanel = mP;
		this.roomIcon = "picture/menu_pics/roomBar.png";
		this.rooms = rooms;
		
		Color wh = Color.white;
		//wh.getTransparency();
		this.setBackground(wh);
		
		
		initComponents();
		//this.repaint();
	}
	
    /**
     * Metoda rysujaca panel z pokojami
     */
	@Override
	public void paintComponent(Graphics g) {
	    Graphics2D g2 = (Graphics2D) g;
	    
	    
	    Image imgd = null;
		try {
			imgd = ImageIO.read(new File(roomIcon));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g22 = img.createGraphics();
		g22.drawImage(imgd, null, null);

		int w = img.getWidth(null);
		int h = img.getHeight(null);
		BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
		Graphics tempG = bi.getGraphics();
		tempG.drawImage(img, 0, 0, null);
		
		float[] scales = { 0.5f, 0.5f, 0.5f, 0.3f };
		float[] offsets = new float[4];
		RescaleOp rop = new RescaleOp(scales, offsets, null);

		/* Draw the image, applying the filter */
		g2.drawImage(bi, rop, 0, 0);
	    
		
		
	}
	
	public void setRooms(ArrayList<Room> roomowie) {
		rooms = roomowie;
		this.setPreferredSize(new Dimension(width,heigh * rooms.size()));
		
		initComponents();
		this.repaint();
	}

	private void initComponents() {
		
		buttons = new ArrayList<ImageButton>();
		this.setPreferredSize(new Dimension(width,heigh * rooms.size()));
		
	
		for(int i = 0; i < rooms.size(); i++) {
		ImageButton baton = new ImageButton(roomIcon,roomIcon);
		JLabel name = new JLabel();
		name.setText(rooms.get(i).getName());
		
		JLabel user = new JLabel();
		user.setText(rooms.get(i).getPlayers().length+"/"+rooms.get(i).getNumPlay());
		final int number = i;

    	name.setBounds(40, 30 + (i * heigh), name.getWidth(),name.getHeight());
    	name.setBounds(220, 30 + (i * heigh), name.getWidth(),name.getHeight());
    	baton.setBounds(20, 10 + (i * heigh),251,62);	
	
		baton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAction(evt,number);
            }
        });
		
		this.add(baton);
		this.add(name);
		this.add(user);
		//this.add(baton);
		}
	}
	
	public void buttonAction(ActionEvent e, int i) {
		//TODO Send request to server with connect to room number i
		motherPanel.setVisible(false);
		
		System.out.println("KLIK");
		System.exit(-1);
	}
	
}
